<template>
	<view class="sales-home">
		<!-- <template v-if="$userInfo.role == 4 || $userInfo.role == 5">
			<zjDom />
		</template> -->
		<template v-if="$userInfo.role!=6">
			<!-- 顶部导航 -->

			<view class="page-header">
				<status-bar />
				<view class="header-title">新阳光广场</view>
				<view class="header-content">
					<view class="user-info">
						<view class="avatar-section">
							<view class="avatar">
								<text class="avatar-text">{{ $userInfo.nickName.charAt(0) }}</text>
							</view>
						</view>
						<view class="user-details">
							<text class="username">{{ $userInfo.nickName }} <uni-tag size="mini"
									v-if="$userInfo.role == 3" :text="$userInfo.isOnline ? '在岗' : '离岗'"
									:type="$userInfo.isOnline?'success':'error'" :circle="true"></uni-tag></text>
							<text class="role">{{ getUserRole() }}</text>
							<!-- <view class="online-status"
											v-if="$userInfo.role != 4 && $userInfo.role !=5 && $userInfo.role !=1">
											<uni-tag size="mini" :text="$userInfo.isOnline ? '在岗' : '离岗'"
												:type="$userInfo.isOnline?'success':'error'" :circle="true"></uni-tag>
										</view> -->
						</view>
					</view>
					<view class="stats" v-if="$userInfo.role == 3">
						<view class="stat-item">
							<text class="stat-value">{{ todyTotal }}</text>
							<text class="stat-label">今日接待</text>
						</view>
						<!-- <view class="stat-item">
										<text class="stat-value">{{ stats.monthSales }}</text>
										<text class="stat-label">本月成交</text>
									</view> -->
					</view>
					<view class="stats" v-if="$userInfo.role == 7 || $userInfo.role == 8">
						<view class="stat-item">
							<text class="stat-value">{{ totalRe }}</text>
							<text class="stat-label">今日认购</text>
						</view>
						<view class="stat-item">
							<text class="stat-value">{{ totalPrice }}</text>
							<text class="stat-label">今日销售</text>
						</view>
					</view>
				</view>
			</view>


			<!-- 快捷操作 -->
			<!-- // 置业顾问 新建报备库 渠道：电话联系，微信联系，朋友推荐 -->
			<view class="quick-actions" v-if="$userInfo.role ==3">
				<view class="action-item" @click="addNewCustomer">
					<view class="action-icon ">
						<image src="@/static/imgs/add.png"></image>
					</view>
					<text class="action-text">来电报备</text>
				</view>
				<view class="action-item" @click="viewAllHistory(3)">
					<view class="action-icon">
						<image src="@/static/imgs/customer-ac.png"></image>
					</view>
					<text class="action-text">来电客户</text>
				</view>
				<view class="action-item" @click="viewProperties">
					<view class="action-icon">
						<image src="@/static/imgs/build.png"></image>
					</view>
					<text class="action-text">房源信息</text>
				</view>
			</view>
			
			<view class="quick-actions" v-if="$userInfo.role ==1 || $userInfo.role ==7 || $userInfo.role ==8">
			<!-- 	<view class="action-item" @click="addNewCustomer">
					<view class="action-icon ">
						<image src="@/static/imgs/add.png"></image>
					</view>
					<text class="action-text">来电报备</text>
				</view> -->
				<view class="action-item" @click="myManager(3)">
					<view class="action-icon">
						<image src="@/static/imgs/customer-ac.png"></image>
					</view>
					<text class="action-text">我的团队</text>
				</view>
				<view class="action-item" @click="addFlow">
					<view class="action-icon">
						<image src="@/static/imgs/liushui.png"></image>
					</view>
					<text class="action-text">款项录入</text>
				</view>
				<view class="action-item" @click="viewProperties">
					<view class="action-icon">
						<image src="@/static/imgs/build.png"></image>
					</view>
					<text class="action-text">房源信息</text>
				</view>
			</view>

			<template v-if="$userInfo.role == 1 || $userInfo.role == 7 || $userInfo.role == 8">
				<view class="reser section">
					<view class="section-header">
						<text class="section-title">待审批-认购</text>
						<text class="section-subtitle">正在认购的单子</text>
						<uni-badge class="uni-badge-left-margin" :text="reservationList.length" />
						<!-- <text class="section-badge" v-if="reservationList.length">{{ reservationList.length }}</text> -->
					</view>

					<view class="customer-list" v-if="reservationList.length > 0">
						<view class="customer-card" v-for="item in reservationList" :key="item._id">
							<view class="card-header">
								<view class="house-info">
									<text
										class="house-address">{{ item.houseInfo.buildingNumber }}栋{{ item.houseInfo.roomNumber }}</text>
								</view>
								<view class="action-buttons">
									<button class="btn btn-outline btn-mid" @tap.stop="handleApproe(item)">审批</button>
								</view>
							</view>

							<!-- 房屋基本信息 -->
							<view class="basic-info">
								<view class="info-row">
									<view class="info-item">
										<text class="info-label">面积</text>
										<text class="info-value">{{ item.houseInfo.area }}㎡</text>
									</view>
									<view class="info-item">
										<text class="info-label">楼层</text>
										<text class="info-value">{{ item.houseInfo.floor }}F</text>
									</view>
								</view>
							</view>

							<view class="basic-info">
								<view class="info-row">
									<view class="info-item">
										<text class="info-label">原价</text>
										<text class="info-value">{{ item.houseInfo.totalPrice }}</text>
									</view>
									<view class="info-item">
										<text class="info-label">原单价</text>
										<text class="info-value">{{ item.houseInfo.unitPrice }}</text>
									</view>
								</view>
							</view>

							<!-- 价格信息 -->
							<view class="price-info">
								<view class="price-item">
									<text class="price-label">申请总价</text>
									<text class="total-price">{{ item.applyPrice}} </text>
								</view>
								<view class="price-item">
									<text class="price-label">折扣</text>
									<text class="unit-price">{{ item.applyZK }}折{{item.otherPrice}} </text>
								</view>
							</view>

							<view class="info-item">资料</view>
							<view class="images">
								<image class="image-item" :src="one.path" @tap="showImage(item.imageArr,index)"
									v-for="(one,index) in item.imageArr"></image>
							</view>
							<view class="price-info">
								<view class="price-item">
									<text class="price-label">定金</text>
									<text class="total-price">{{ $formatPrice(item.deposit) }}</text>
								</view>
							</view>

							<!-- 时间信息 -->
							<view class="time-info">
								<view class="time-item">
									<uni-icons type="calendar" size="14" color="#999"></uni-icons>
									<text class="time-text">预约时间：{{ $formatTime(item.reservationTime) }}</text>
								</view>
							</view>

							<!-- 操作按钮 -->

						</view>
					</view>

					<view class="empty-state" v-else>
						<image src="@/static/imgs/empty.png"></image>
						<text class="empty-text">暂无认购的单子</text>
						<!-- <button class="empty-btn" @click="addNewCustomer">添加客户</button> -->
					</view>
				</view>
				
				
				<view class="reser section">
					<view class="section-header">
						<text class="section-title">待审批-签约</text>
						<text class="section-subtitle">正在签约的单子</text>
						<uni-badge class="uni-badge-left-margin" :text="signList.length" />
						<!-- <text class="section-badge" v-if="signList.length">{{ signList.length }}</text> -->
					</view>
				
					<view class="customer-list" v-if="signList.length > 0">
						<view class="customer-card" v-for="item in signList" :key="item._id">
							<view class="card-header">
								<view class="house-info">
									<text
										class="house-address">{{ item.houseInfo.buildingNumber }}栋{{ item.houseInfo.roomNumber }}</text>
								</view>
								<view class="action-buttons">
									<button class="btn btn-outline" @tap.stop="handleApproe(item)">审批</button>
								</view>
							</view>
				
							<!-- 房屋基本信息 -->
							<view class="basic-info">
								<view class="info-row">
									<view class="info-item">
										<text class="info-label">面积</text>
										<text class="info-value">{{ item.houseInfo.area }}㎡</text>
									</view>
									<view class="info-item">
										<text class="info-label">楼层</text>
										<text class="info-value">{{ item.houseInfo.floor }}F</text>
									</view>
								</view>
							</view>
				
							<view class="basic-info">
								<view class="info-row">
									<view class="info-item">
										<text class="info-label">原价</text>
										<text class="info-value">{{ item.houseInfo.totalPrice }}</text>
									</view>
									<view class="info-item">
										<text class="info-label">原单价</text>
										<text class="info-value">{{ item.houseInfo.unitPrice }}</text>
									</view>
								</view>
							</view>
				
							<!-- 价格信息 -->
							<view class="price-info">
								<view class="price-item">
									<text class="price-label">申请总价</text>
									<text class="total-price">{{ item.applyPrice}} </text>
								</view>
								<view class="price-item">
									<text class="price-label">折扣</text>
									<text class="unit-price">{{ item.applyZK }}折{{item.otherPrice}} </text>
								</view>
							</view>
				
							<view class="info-item">资料</view>
							<view class="images">
								<image class="image-item" :src="one.path" @tap="showImage(item.imageArr,index)"
									v-for="(one,index) in item.imageArr"></image>
							</view>
							<view class="price-info">
								<view class="price-item">
									<text class="price-label">定金</text>
									<text class="total-price">{{ $formatPrice(item.deposit) }}</text>
								</view>
							</view>
				
							<!-- 时间信息 -->
							<view class="time-info">
								<view class="time-item">
									<uni-icons type="calendar" size="14" color="#999"></uni-icons>
									<text class="time-text">预约时间：{{ $formatTime(item.reservationTime) }}</text>
								</view>
							</view>
				
							<!-- 操作按钮 -->
				
						</view>
					</view>
				
					<view class="empty-state">
						<image src="@/static/imgs/empty.png"></image>
						<text class="empty-text">暂无签约的单子</text>
						<!-- <button class="empty-btn" @click="addNewCustomer">添加客户</button> -->
					</view>
				</view>

				<!-- <view class=" section">
					<view class="section-header">
						<text class="section-title">等待接待</text>
						<text class="section-subtitle"></text>
						<text class="section-badge">{{ waitCustomers.length }}</text>
					</view>

					<view class="customer-list" v-if="waitCustomers.length > 0">
						<view class="customer-card" v-for="customer in waitCustomers" :key="customer._id"
							@click="viewCustomerDetail(customer)">
							<view class="customer-header">
								<image class="customer-avatar" :src="customer.avatar" mode="aspectFill"></image>
								<view class="customer-info">
									<text class="customer-name">{{ customer.customerName }}</text>
									<text class="customer-phone">{{ customer.phone }}</text>
								</view>
								<view class="customer-tag" :class="customer.priority">
									{{ customer.priority === 'high' ? '高意向' : customer.priority === 'medium' ? '中意向' : '普通' }}
								</view>
							</view>

							<view class="customer-details" v-if="customer.interestedType || customer.budget">
								<view class="detail-item">
									<text class="detail-label">关注户型</text>
									<text class="detail-value">{{ customer.interestedType }}</text>
								</view>
								<view class="detail-item">
									<text class="detail-label">预算范围</text>
									<text class="detail-value">{{ customer.budget }}</text>
								</view>
							</view>

							<view class="customer-actions">
								<text class="visit-time">上次分配时间: {{ $formatTime(customer.visitTime) }}</text>
								<view class="action-buttons">
									<button class="btn-small primary" @tap.stop="fenPeiCustomer(customer)">分配顾问</button>
								</view>
							</view>
						</view>
					</view>

					<view class="empty-state" v-else>
						<text class="empty-icon">👥</text>
						<text class="empty-text">暂无正在接待的客户</text>
						<button class="empty-btn" @click="addNewCustomer">添加客户</button>
					</view>
				</view> -->
			</template>

			<!-- // 置业顾问 -->
			<template v-else>
				<!-- 当前接待客户 -->
				<!-- 
				<view class=" section">
					<view class="section-header">
						<text class="section-title">待接待客户</text>
						<uni-badge class="uni-badge-left-margin" :text="currentCustomers.length" />
					</view>

					<view class="customer-list" v-if="currentCustomers.length > 0">
						<view class="customer-card" v-for="customer in currentCustomers" :key="customer._id"
							@click="viewCustomerDetail(customer)">
							<view class="customer-header">
								<view class="customer-info">
									<text class="customer-name">{{ customer.customerName }}</text>
									<text class="customer-phone">{{ customer.phone }}</text>
								</view>
								<view class="customer-tag" :class="customer.priority">
									{{ customer.priority === 'high' ? '高意向' : customer.priority === 'medium' ? '中意向' : '普通' }}
								</view>
							</view>

							<view class="customer-details" v-if="customer.interestedType || customer.budget">
								<view class="detail-item">
									<text class="detail-label">关注户型</text>
									<text class="detail-value">{{ customer.interestedType }}</text>
								</view>
								<view class="detail-item">
									<text class="detail-label">预算范围</text>
									<text class="detail-value">{{ customer.budget }}</text>
								</view>
							</view>

							<view class="customer-actions">
								<text class="visit-time">上次到访: {{ $formatTime(customer.visitTime) }}</text>
								<view class="action-buttons">
									<button class="btn-small primary" @tap.stop="contactCustomer(customer)">联系</button>
									<button class="btn-small secondary" @tap.stop="saveCustomer(customer)">接待</button>
								</view>
							</view>
						</view>
					</view>

					<view class="empty-state" v-else>
						<image src="@/static/imgs/empty.png"></image>
						<text class="empty-text">暂无待接待客户</text>
					</view>
				</view>
				<view class=" section">
					<view class="section-header">
						<text class="section-title">当日接待客户</text>
						<uni-badge class="uni-badge-left-margin" :text="todyTotal" />
						<text style="flex:1;"></text>
						<text class="see-all" @click="viewAllHistory(1)">查看全部</text>
					</view>

					<view class="customer-list" v-if="todyCustomers.length > 0">
						<view class="customer-card" v-for="customer in todyCustomers" :key="customer._id"
							@click="viewCustomerDetail(customer)">
							<view class="customer-header">
								<view class="customer-info">
									<text class="customer-name">{{ customer.customerName }}</text>
									<text class="customer-phone">{{ customer.phone }}</text>
								</view>
								<view class="customer-tag">
									{{$getLevel(customer.intentionLevel)}}
								</view>
							</view>

							<view class="customer-details" v-if="customer.interestedType || customer.budget">
								<view class="detail-item">
									<text class="detail-label">关注户型</text>
									<text class="detail-value">{{ customer.interestedType }}</text>
								</view>
								<view class="detail-item">
									<text class="detail-label">预算范围</text>
									<text class="detail-value">{{ customer.budget }}</text>
								</view>
							</view>

							<view class="customer-actions">
								<text class="visit-time">上次到访: {{ $formatTime(customer.visitTime) }}</text>
							</view>
						</view>
					</view>

					<view class="empty-state" v-else>
						<image src="@/static/imgs/empty.png"></image>
						<text class="empty-text">暂无接待客户</text>
					</view>
				</view>
				 -->
				 
				 <view class="section">
				 	<view class="section-header">
				 		<text class="section-title">待办事项</text>
				 	</view>
				 	<view class="history-list">
						
						<uni-list-item title="老客户待跟进" note="3天内到期的客户" showBadge="true" clickable  :badgeText="expirteNum" showArrow="true" badgeType="error" @click="linkCustomerList(expirteNum,'expirteNum')"></uni-list-item>
						<uni-list-item title="新客户待处理" note="还未填写客户信息的客户" clickable  showBadge="true" :badgeText="waitNum" showArrow="true" badgeType="error" @tap="linkCustomerList(waitNum,'waitNum')"></uni-list-item>
						<uni-list-item title="来访待处理" note="今天到访还未接待的客户" clickable  showBadge="true" :badgeText="waitRes" showArrow="true" badgeType="error" @tap="linkCustomerList(waitRes,'waitRes')"></uni-list-item>
						<!-- <uni-list-item title="开启点击反馈" clickable  @click="onClick" ></uni-list-item>
						<uni-list-item title="开启点击反馈" clickable  @click="onClick" ></uni-list-item>
				 		<view class="list-info">
							<view class="label">新客户待处理 <uni-badge type="error" :text="waitNum"></uni-badge></view>
						</view>
						<view class="list-info">
							<view class="label">老客户待跟进<uni-badge :text="expirteNum" type="error"></uni-badge></view>
						</view>
						<view class="list-info">
							<view class="label">来访待处理 <uni-badge :text="waitRes" type="error"></uni-badge></view>
						</view> -->
				 	</view>
				 </view>
				
				<view class="section">
					<view class="section-header">
						<text class="section-title">来电客户</text>
						<uni-badge class="uni-badge-left-margin" :text="midTotal" />
						<text style="flex:1;"></text>
						<text class="see-all" @click="viewAllHistory(4)">查看全部</text>
					</view>

					<view class="history-list">
						<view class="history-item" v-for="customer in midList" :key="customer._id"
							@click="viewCustomerDetail(customer)">
							<view class="history-content">
								<view class="history-header">
									<text class="history-name">{{ customer.customerName }}</text>
									<!-- <text class="customer-status-tag" :class="$getStatusClass(customer.status)">
										{{ $getStatusText(customer.status) }}
									</text> -->
								</view>
								<text class="history-phone">{{ customer.phone }}</text>
							</view>
							<view class="customer-actions">
								<text class="visit-time">
									<template v-if="customer.scheduleDateTime">
										上次报备: {{$formatTime( customer.scheduleDateTime) }}
									</template>
								</text>
								<view class="action-buttons">
									<!-- <button class="btn-small primary" @tap.stop="getQrCode(customer)">二维码</button> -->
									<button class="btn-small primary" @tap.stop="contactCustomer(customer)">联系</button>
									<button class="btn-small secondary" @tap.stop="scheduleVisit(customer)">报备</button>
								</view>
							</view>
						</view>
						
						<view class="empty-state" v-if="midList.length === 0">
							<image src="@/static/imgs/empty.png"></image>
							<text class="empty-text">今日无来电客户</text>
							<!-- <button class="empty-btn" @click="addNewCustomer">添加客户</button> -->
						</view>
					</view>
				</view>
			</template>

			<!-- 前台 -->
		</template>

		<!-- // 审批认购 -->
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" title="审批理由" cancelText="不通过" confirmText="通过"
				@confirm="cancelAppore('add')" :before-close="true" @close="cancelAppore('cancel')">
				<view style="flex:1;width:100%" class="inputDialog-content">
					<textarea class="pop-textarewa" v-model="textArea" auto-height placeholder="请输入理由" />
				</view>
			</uni-popup-dialog>
		</uni-popup>


		<!-- 成功提示 -->
		<uni-popup ref="addPeople" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="电话号码" :beforeClose="true" @close="closeFun"
				placeholder="请输入电话号码" @confirm="dialogInputConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {},
				stats: {
					todayVisitors: 8,
					monthSales: 12
				},
				currentCustomers: [],
				historyCustomers: [],
				reservationList: [],
				rPageNo: 1,
				curReservation: {},
				textArea: '',
				waitCustomers: [],
				midList: [],
				midTotal: '',
				todyTotal: '',
				todyCustomers: [],
				totalRe:0.,
				totalPrice:0,
				signList:[],
				waitNum:'',
				waitRes:'',
				expirteNum:''
			}
		},
		onShow() {
			let appUser = this.$userInfo
			if (!appUser.role) {
				uni.reLaunch({
					url: "/pages/login/login"
				})
			}
			let role = appUser.role
			// 中介
			if (role == 4 || role == 5) {
				uni.navigateTo({
					url: "/pages/main/zhongjie"
				})
			}
			if (role == 6) {
				uni.redirectTo({
					url: "/pages/main/qiantai"
				})
			}

			this.loadSalesData()
		},
		onLoad() {
			uni.startPullDownRefresh();
		},
		onPullDownRefresh() {
			this.loadSalesData()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1000)
		},

		methods: {
			myManager(){
				uni.navigateTo({
					url:"/pages/profile/manager"
				})
			},
			handleApproe(item) {
				this.curReservation = item
				this.$refs.inputDialog.open()
			},
			getWaitCustomers() {
				this.$cloudOBjAjax('suuny-custom', 'getCustomer', {
					isRegistration: 1,
					pageSize: 1000
				}).then(res => {
					this.waitCustomers = res.list
				})
			},
			apporAjax(val) {
				this.$cloudOBjAjax('house-reservation', 'updateStatus', {
					reservationId: this.curReservation._id,
					status: val,
					remark: this.textArea
				}).then(res => {
					this.curReservation = {}
					// console.log('close',this.$refs.inputDialog)
					this.$refs.inputDialog.close()
					this.rPageNo = 1
					this.getReservation()
					uni.showToast({
						title: "提交成功",
						icon: "success"
					})
				}).catch(err => {
					console.log('errr', err)
					uni.showToast({
						title: err.errMsg || '出错了，请联系管理员',
						icon: "success"
					})
					// this.curReservation = {}
				})

				// 认购成功去更新客户状态
				if (status === 4) {
					let customer = this.$cloudOBjAjax('sunny-customer', 'updateStatus', {
						_id: ''
					})
				}

			},
			// 1: '已预订', // 等待经理初步审核价格
			// 2: '已签约', // 经理审核通过,等待上传认购资料
			// 3: "上传资料待审核",//上传资料待审核
			// 4: '已认购', //审核通过"
			// 5: '审核不通过',//初步审核不通过
			// 6: '资料审核不通过',//资料审核不通过
			cancelAppore(type) {
				const item = this.curReservation
				let status
				if (type === 'cancel') {
					if (!this.textArea) {
						return this.$simpleToast('请输入审批理由')
					}
					status = 4
				} else {
					status = item.status * 1 + 1
				}
				this.apporAjax(status)
			},
			// 加载销售数据
			loadSalesData() {
				// 这里可以调用API获取真实数据
				console.log('加载销售数据...')
				// role 
				// "description": "角色 1管理员 2买家 3销售人员 4中介 5个人中介 6前台",
				let appUser = this.$userInfo
				// 置业顾问
				if (appUser.role === 3) {
					this.getUserCustom()
					this.getMidCustom()
				} else if (appUser.role === 1 || appUser.role === 7 || appUser.role === 8) {
					this.getReservation()
					// this.getWaitCustomers()
				}

			},

			getUserCustom() {
				//带接待的客户
				this.$cloudOBjAjax('suuny-custom', 'getZYGWcustomer', {
					userId: this.$userInfo._id,
					isRegistration: 1,
					pageSize: 3,
				}).then(res => {
					this.currentCustomers = res.list
					this.waitRes = res.total
				})

				// 今天接待的客户
				this.$cloudOBjAjax('suuny-custom', 'getZYGWcustomer', {
					userId: this.$userInfo._id,
					registrationDate: this.$formatYear(new Date()),
					pageSize: 3,
				}).then(res => {
					this.todyCustomers = res.list
					this.todyTotal = res.total
				})
				
				// 待跟进的客户
				this.$cloudOBjAjax('suuny-custom', 'getZYGWcustomer', {
					userId: this.$userInfo._id,
					pageSize: 3,
					completionRate:0
				}).then(res => {
					console.log('res待跟进',res)
					this.waitNum = res.total
				})
				
				// 老客户待跟进 就是过期时间还差2天
				console.log('onShow')
				this.$cloudOBjAjax('suuny-custom', 'getZYGWcustomer', {
					userId: this.$userInfo._id,
					pageSize: 3,
					expirteDay:3
				}).then(res => {
					console.log('res待跟进2',res)
					this.expirteNum = res.total
				})

			},


			async getMidCustom() {
				const res = await this.$cloudOBjAjax('middleman-advisor-customers', 'getCustomerList', {
					middlemanId: this.$userInfo._id,
					isSchedule: 0, // 未报备
					pageSize: 3
				});

				this.midList = res.list
				this.midTotal = res.total
			},
			// getUserHisCustom() {
			// 	this.$cloudOBjAjax('suuny-custom', 'getZYGWcustomer', {
			// 		userId: this.$userInfo._id,
			// 		pageSize: 3
			// 	}).then(res => {
			// 		let users = res
			// 		this.historyCustomers = res.list

			// 	})
			// },
			changeUserOnline() {
				this.$cloudOBjAjax('sunny-users', 'toggleUserOnline', {
					userId: this.$userInfo._id
				}).then(res => {
					let userInfo = this.$userInfo
					userInfo.isOnline = userInfo.isOnline ? 0 : 1
					this.$store.commit('app/setUserInfo', userInfo)
				})

			},
			getUserRole() {
				let appUser = this.$userInfo
				let role = appUser.role
				let res = ''
				switch (role) {
					case 3:
						res = '高级置业顾问'
						break;
					case 4:
					case 5:
						res = appUser.agency
						break;
					default:
						break;
				}

				console.log(role)
				return res
			},

			// 添加新客户
			addNewCustomer() {
				this.$refs.addPeople.open()
			},
			closeFun() {
				this.$refs.addPeople.close()
			},
			async dialogInputConfirm(value) {
				let isTest = !/^1[3-9]\d{9}$/.test(value)
				if (isTest) {
					return this.$simpleToast('电话号码错误')
				}

				if (this.$userInfo.role === 3) {

					this.$cloudOBjAjax('middleman-advisor-customers', 'checkCustomerExists', {
						phone: value,
						middlemanId: this.$userInfo._id
					}).catch(err => {
						this.$simpleToast(err.errMsg || '出错了,请联系管理员')
					}).then(res => {
						if (res.exists) {
							this.$simpleToast('该客户已经被注册了')
						} else {
							this.closeFun()
							uni.navigateTo({
								url: "/pages/customer/addByadvisor?phone=" + value
							})
						}

					}).catch(err => {
						this.$simpleToast(err.errMsg || '出错了,请联系管理员')
					})
					return
				}

				this.$cloudOBjAjax('suuny-custom', 'getCustomerByPhone', {
					phone: value
				}).catch(err => {
					this.$simpleToast(err.errMsg || '出错了,请联系管理员')
				}).then(res => {
					// this.$simpleToast('该客户已经注册了')
					// 未注册
					if (res.length === 0) {
						this.closeFun()
						uni.navigateTo({
							url: "/pages/customer/add?phone=" + value
						})
					} else {
						this.$simpleToast('该客户已经被注册了')
					}

				})

			},

			// 查看日程
			viewSchedule() {
				uni.navigateTo({
					url: '/pages/schedule/index'
				})
			},

			// 查看业绩
			viewPerformance() {
				uni.navigateTo({
					url: '/pages/performance/index'
				})
			},

			// 查看房源
			viewProperties() {
				uni.navigateTo({
					url: '/pages/properties/index'
				})
			},

			// 查看客户详情
			viewCustomerDetail(customer) {
				uni.navigateTo({
					url: `/pages/customer/detail?id=${customer._id}`
				})
			},

			// 联系客户
			contactCustomer(customer) {
				uni.makePhoneCall({
					phoneNumber: customer.phone
				})
			},

			// 安排约访
			async scheduleVisit(customer) {
				let curTime = this.$formatYear(new Date())
				await this.$cloudOBjAjax('customer-lifecycle-history', 'addZJSchedule', {
					customerId: customer._id,
					schedule_time: curTime
				}).then(res => {
					this.$simpleToast('预约成功')
					this.getMidCustom()
				})
			},

			// 查看全部历史
			viewAllHistory(type) {
				switch (type) {
					case 1:
						uni.navigateTo({
							url: '/pages/customer/my?type=1'
						})
						break;
					case 2:
						uni.navigateTo({
							url: '/pages/customer/my?type=2'
						})
						break;
						// 未报备的客户
					case 4:
						uni.navigateTo({
							url: '/pages/customer/zjList?type=11'
						})
						break;
					default:
						uni.navigateTo({
							url: '/pages/customer/zjList'
						})
						break;
				}
				return
				if (!type) {
					uni.navigateTo({
						url: '/pages/customer/zjList?type=1'
					})
				} else {
					uni.navigateTo({
						url: '/pages/customer/zjList'
					})
				}

			},

			// 加载更多历史记录
			loadMoreHistory() {
				// 加载更多历史客户数据
				uni.showLoading({
					title: '加载中...'
				})

				setTimeout(() => {
					// 模拟加载更多数据
					this.historyCustomers.push(...this.getMoreHistoryData())
					uni.hideLoading()
				}, 1000)
			},

			// 切换在岗状态
			toggleOnlineStatus() {
				this.isOnline = !this.isOnline
				uni.showToast({
					title: this.isOnline ? '您已上线' : '您已离岗',
					icon: 'none'
				})
				// 这里可以添加API调用，更新服务器状态
			},

			// 导航到其他页面
			navigateTo(page) {
				const routes = {
					customers: '/pages/customer/list',
					schedule: '/pages/schedule/index',
					profile: '/pages/profile/index'
				}

				if (routes[page]) {
					uni.switchTab({
						url: routes[page]
					})
				}
			},
			async getReservation() {
				let role = this.$userInfo.role
				let status = [1, 2]
				if (role == 7) {
					status = 1
				}
				if (role == 8) {
					status = 2
				}
				await this.$cloudOBjAjax('house-reservation', 'getList', {
					status: status
				}).then(res => {
					this.reservationList = res.list
				})
				
				let time = this.$getTodayTimeRange()
				await this.$cloudOBjAjax('house-reservation', 'getList', {
					status: 3,
						startTime:time.startTime,
						endTime: time.endTime
				}).then(res => {
					console.log(res)
				})
				
			},
			showImage(arr, index) {
				let imgs = arr.map(item => {
					return item.path
				})

				uni.previewImage({
					urls: imgs,
					current: index
				})
			},
			async saveCustomer(item) {

				const bangdGX = await this.$cloudOBjAjax('suuny-custom', 'saveConsultant', {
					customerId: item._id,
					customerName: this.$userInfo.nickName,
					consultantId: this.$userInfo._id
				})

				let postData = {
					customerId: item._id,
					eventType: 2,
					customerName: item.customerName,
					consultantId: this.$userInfo._id
				}

				const res = await this.$cloudOBjAjax('customer-lifecycle-history', 'addWaitRecord', postData)
				uni.navigateTo({
					url: `/pages/customer/detail?id=${item._id}`
				})
			},
			fenPeiCustomer(item) {
				let curCustomer = uni.setStorageSync('curCustom', curCustomer)
				uni.navigateTo({
					url: "/pages/customer/fenpei?id=" + item.id + "enventType=" + 20
				})
			},
			addFlow(){
				uni.showActionSheet({
					title:"流水方式",
					itemList:['认购','签约','贷款','首付'],
					success:res=>{
						uni.navigateTo({
							url:"/pages/flow/add?type=" + res.tapIndex+1
						})
					}
					
				})
			},
			linkCustomerList(item){
				console.log(item)
				if(!item){
					return this.$simpleToast('没有待办事项')
				}
				uni.navigateTo({
					url:"/pages/customer/constList"
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.sales-home {
		background: #f5f6fa;
		padding-bottom: 20rpx;
		min-height: 100%;
		padding-bottom: var(--window-bottom);
	}


	.online-status {
		margin-top: 10rpx;
	}

	.status-btn {
		padding: 6rpx 20rpx;
		border-radius: 20rpx;
		font-size: 22rpx;
		border: none;
		color: white;

		// &.online {
		//   background: #4cd964;
		// }

		// &.offline {
		//   background: #ff4757;
		// }
	}

	.username {
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 8rpx;
	}

	.role {
		font-size: 24rpx;
		opacity: 0.9;
	}

	.stats {
		display: flex;
		gap: 40rpx;
	}

	.stat-item {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.stat-value {
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 8rpx;
	}

	.stat-label {
		font-size: 22rpx;
		opacity: 0.9;
	}

	/* 快捷操作 */
	.quick-actions {
		display: flex;
		justify-content: space-around;
		background: white;
		margin: -160rpx 40rpx 30rpx;
		padding: 30rpx;
		border-radius: 20rpx;
		box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
	}

	.action-item {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.action-icon {
		// width: 60rpx;
		// height:60rpx;
		image {
			width: 60rpx;
			height:60rpx;
		}
	}

	.action-text {
		font-size: 32rpx;
		color: #333;
	}

	/* 区域样式 */
	.section {
		background: white;
		margin: 20rpx 20rpx;
		border-radius: 20rpx;
		padding: 20rpx;
		box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
	}

	.section-header {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.section-title {
		font-size: 28rrpx;
		color: #333;
		margin-right: 16rpx;
	}

	.section-subtitle {
		font-size: 24rpx;
		color: #999;
		flex: 1;
	}

	.section-badge {
		background: #ff4757;
		color: white;
		padding: 4rpx 8rpx;
		border-radius: 20rpx;
		font-size: 20rpx;
	}

	.see-all {
		color: #999;
		font-size: 24rpx;
	}

	/* 当前客户列表 */
	.customer-list {
		display: flex;
		gap: 30rpx;
		white-space: nowrap;
		flex-direction: column;
	}

	.customer-card {
		min-width: 400rpx;
		background: #f8f9fa;
		border-radius: 16rpx;
		padding: 20rpx;
		border-left: 6rpx solid #667eea;
		margin-bottom: 10rpx;
	}

	.customer-header {
		display: flex;
		align-items: center;
		margin-bottom: 24rpx;
	}

	.customer-avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.customer-info {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.customer-name {
		font-size: 28rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 8rpx;
	}

	.customer-phone {
		font-size: 22rpx;
		color: #666;
	}

	.customer-tag {
		padding: 8rpx 16rpx;
		border-radius: 12rpx;
		font-size: 20rpx;

		&.high {
			background: #ff4757;
			color: white;
		}

		&.medium {
			background: #ffa502;
			color: white;
		}

		&.low {
			background: #dfe4ea;
			color: #666;
		}
	}

	.customer-details {
		margin-bottom: 24rpx;
	}

	.detail-item {
		display: flex;
		justify-content: space-between;
		margin-bottom: 12rpx;
	}

	.detail-label {
		font-size: 24rpx;
		color: #666;
	}

	.detail-value {
		font-size: 24rpx;
		color: #333;
		font-weight: 500;
	}

	.customer-actions {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.visit-time {
		font-size: 22rpx;
		color: #999;
	}

	.action-buttons {
		display: flex;
		gap: 16rpx;
	}

	.btn-small {
		padding: 0 40rpx;
		border-radius: 4rpx;
		font-size: 22rpx;
		border: none;
		line-height: 40rpx;

		&.primary {
			background: #667eea;
			color: white;
		}

		&.secondary {
			background: #f1f2f6;
			color: #666;
		}
	}


	.empty-btn {
		background: #667eea;
		color: white;
		border: none;
		padding: 20rpx 40rpx;
		border-radius: 25rpx;
		font-size: 26rpx;
	}

	/* 历史客户列表 */
	.history-list {
		display: flex;
		flex-direction: column;
		gap: 30rpx;
		overflow-y: auto;
		max-height: calc(100vh - 700rpx);
		padding-bottom: 20rpx;
	}

	.history-item {
		display: flex;
		align-items: center;
		padding: 30rpx;
		background: #f8f9fa;
		border-radius: 16rpx;
	}

	.history-avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-right: 24rpx;
	}

	.history-content {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.history-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 8rpx;
	}

	.history-name {
		font-size: 28rpx;
		font-weight: bold;
		color: #333;
	}

	.history-status {
		padding: 6rpx 16rpx;
		border-radius: 12rpx;
		font-size: 20rpx;

		&.purchased {
			background: #2ed573;
			color: white;
		}

		&.negotiating {
			background: #ffa502;
			color: white;
		}

		&.not_purchased {
			background: #dfe4ea;
			color: #666;
		}
	}

	.history-phone {
		font-size: 24rpx;
		color: #666;
		margin-bottom: 8rpx;
	}

	.history-date {
		font-size: 22rpx;
		color: #999;
	}

	.history-amount {
		text-align: right;
	}

	.amount {
		font-size: 28rpx;
		font-weight: bold;
		color: #ff4757;
	}

	/* 加载更多 */
	.load-more {
		text-align: center;
		padding: 30rpx;
		color: #667eea;
		font-size: 26rpx;
	}

	.avatar-section {
		margin-right: 24rpx;
	}



	.reser.section {
		background: #ffffff;
		border-radius: 12rpx;
		padding: 24rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0.08);

		.section-header {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;

			.section-title {
				font-size: 28rpx;
				font-weight: 600;
				color: #1a1a1a;
				margin-right: 12rpx;
			}

			.section-subtitle {
				font-size: 22rpx;
				color: #666;
				flex: 1;
			}

			.section-badge {
				background: #ff4757;
				color: white;
				padding: 4rpx 12rpx;
				border-radius: 12rpx;
				font-size: 20rpx;
				font-weight: 500;
			}
		}

		.customer-list {
			display: flex;
			flex-direction: column;
			gap: 16rpx;

			.customer-card {
				background: #f8f9fa;
				border-radius: 8rpx;
				padding: 20rpx;
				border: 1rpx solid #e8e8e8;

				.card-header {
					margin-bottom: 10rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.house-info {

						.house-address {
							font-size: 28rpx;
							font-weight: 600;
							color: #1a1a1a;
							display: block;
						}
					}
				}

				.basic-info {
					margin-bottom: 10rpx;

					.info-row {
						display: flex;
						gap: 30rpx;

						.info-item {
							display: flex;

							.info-label {
								font-size: 30rpx;
								color: #666;
								margin-bottom: 4rpx;
								padding-right: 20rpx;
							}

							.info-value {
								font-size: 28rpx;
								color: #333;
								font-weight: 500;
							}
						}
					}
				}

				.price-info {
					display: flex;
					justify-content: space-between;
					align-items: flex-start;
					margin-bottom: 16rpx;
					padding-bottom: 16rpx;
					border-bottom: 1rpx solid #e8e8e8;

					.price-item {
						display: flex;
						align-items: center;

						.price-label {
							font-size: 30rpx;
							color: #666;
							padding-right: 20rpx;
						}

						.total-price {
							font-size: 30rpx;
							color: #ff4757;
							font-weight: bold;
						}

						.unit-price {
							font-size: 30rpx;
							color: #ff4757;
							font-weight: 500;
						}
					}
				}

				.time-info {
					margin-bottom: 16rpx;

					.time-item {
						display: flex;
						align-items: center;
						gap: 8rpx;

						.time-text {
							font-size: 24rpx;
							color: #666;
						}
					}
				}

				.action-buttons {
					display: flex;
					justify-content: flex-end;

					.btn {
						padding: 0rpx 30rpx;
						border-radius: 6rpx;
						font-size: 24rpx;
						font-weight: 500;
						border: none;
						min-width: 80rpx;
						text-align: center;

						&.btn-outline {
							background: linear-gradient(135deg, #FFA500 0%, #FF8C00 100%);
							color: white;
						}
					}
				}
			}
		}
	}

	.images {
		display: flex;
		gap: 20rpx;
		padding: 20rpx;
		;

		.image-item {
			width: 100rpx;
			height: 100rpx;
			border-radius: 10rpx;
		}
	}
</style>